"""
@Author：WangYuXiang
@E-mile：Hill@3io.cc
@CreateTime：2021/4/22 17:00
@DependencyLibrary：无
@MainFunction：无
@FileDoc： 
    qr.py
    文件说明
@ChangeHistory:
    datetime action why
    example:
    2021/4/22 17:00 change 'Fix bug'
        
"""
from apps.school.models import SchoolModel, ClassModel, StudentModel
from apps.school.serializers import SchoolSerializer, ClassSerializer, StudentSerializer
from apps.school.serializers import SearchSerializer
from common.utils.authentication import GeneralTokenAuthenticate
from common.utils.site_views import AdminSiteView
from sanic_rest_framework.filters import ORMAndFilter
from sanic_rest_framework.mixins import ListModelMixin
from sanic_rest_framework.views import ViewSetView


class SchoolAdminView(AdminSiteView):
    queryset = SchoolModel
    serializer_class = SchoolSerializer
    search_fields = ('@name',)


class ClassAdminView(AdminSiteView):
    queryset = ClassModel
    serializer_class = ClassSerializer
    search_fields = ('@name',)


class StudentAdminView(AdminSiteView):
    queryset = StudentModel
    serializer_class = StudentSerializer
    search_fields = ('@name',)


class ClassListAdminView(AdminSiteView):
    queryset = ClassModel
    serializer_class = StudentSerializer
    search_fields = ('@name',)


class SchoolSearchView(ListModelMixin, ViewSetView):
    queryset = SchoolModel
    serializer_class = SearchSerializer
    authentication_classes = (GeneralTokenAuthenticate,)
    filter_class = ORMAndFilter
    search_fields = ('@name', 'id')

    async def list(self, request, *args, **kwargs):
        queryset = await self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        data = await serializer.data
        return self.success_json_response(data=data)


class ClassSearchView(ListModelMixin, ViewSetView):
    queryset = ClassModel
    serializer_class = SearchSerializer
    authentication_classes = (GeneralTokenAuthenticate,)
    filter_class = ORMAndFilter
    search_fields = ('@name', 'id')

    async def list(self, request, *args, **kwargs):
        queryset = await self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        data = await serializer.data
        return self.success_json_response(data=data)
